<template>
	<view>
	<view class="search-box">
		<uni-search-bar  @input="input" :radius="100" cancel-button="none" placeholder="请输入搜索内容"></uni-search-bar>
	</view>
	<!--搜索建议列表 -->
	<view class="sugg-list" v-if="searchtResults.length !==0">
		<view class="sugg-item" v-for="(item,i) in searchtResults" :key="i" @click="gotoDetail(item)">
			<view class="goods_name">{{item.goods_name}}</view>
			<uni-icons type="arrowright" size="16"></uni-icons>
		</view>
	</view>
	<!--搜索历史 -->
	<view class="history-box" v-else>
		<!--标题区域 -->
		<view class="history-title">
			<text>搜索历史</text>
			<uni-icons type="trash" size="17" @click="clean"></uni-icons>
		</view>
		<!--列表区域-->
		<view class="history-list">
			<uni-tag :text="item" v-for="(item,i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
		</view>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer:null,
				kw:null,//搜索关键词
				//搜索列表的结果
				searchtResults:[],
				//搜索历史关键词的 数组
				historyList:[]
			};
		},
		onLoad() {
			this.historyList=JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods:{
			//input输入事件11
			input(e){
				clearTimeout(this.timer)//500毫秒内不执行后面的操作，清除掉timer
			
				this.timer=setTimeout(()=>{//防抖    延时器,500毫秒后执行
					this.kw=e
					this.getSearchList()//搜索的方法
				},500)
				
			},
			getSearchList(){
				//判断搜索关键词是否为空111
				if(this.kw.length === 0){
					this.searchtResults=[]
					return
				}
				uni.request({
					url:'https://api-hmugo-web.itheima.net/api/public/v1/goods/search?query='+this.kw,
					method:'GET',
					success:(res)=>{
						if(res.data.meta.status!==200){
							
							return uni.$showMsg("搜索数据请求失败!")
						}
						console.log(res.data.message)
						this.searchtResults=res.data.message.goods
						this.saveSearchHistory()
					},
					fail() {
						return uni.$showMsg("搜索数据请求失败!")
					}
				})
			},
			//跳转商品详情页
			gotoDetail(item){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
				})
				
			},
			//保存搜索关键词到对应的列表中
			saveSearchHistory(){
				// this.historyList.push(this.kw)
				const set=new Set(this.historyList)//删除搜索关键词重复的数据
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList=Array.from(set)
				//将搜索历史记录持久化存储到本地（持久化  本地存储数据）
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
				
			},
			//清空关键词列表---
			clean(){
				this.historyList=[]
				uni.setStorageSync('kw','[]')
			},
			gotoGoodsList(kw){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query='+kw
				})
			}
		},
		computed:{//计算属性11
			histories(){
				return [...this.historyList].reverse() //基于原数组重新生成一个数组,进行数据反转
			}
			
		}
	}
</script>

<style lang="scss">
		.search-box{
			//吸顶效果
			position: sticky;
			top: 0;
			z-index: 999;
		}
		
		.sugg-list{
			padding: 0 5px;
			.sugg-item{
				display: flex;
				align-items: center;//纵向居中
				justify-content: space-between;//横向 为  两端
				font-size: 12px;
				padding: 13px 0;
				border-bottom: 1px solid #efefef;
				.goods_name{
					
					//文本不允许换行，单行文本
					white-space: nowrap;
					//超出部分隐藏
					overflow: hidden;
					//超出部分使用 ...代替
					text-overflow: ellipsis;
				}
			}
		}
		.history-box{
			padding: 0 5px;
			.history-title{
				display: flex;
				justify-content: space-between;
				height: 40px;
				align-items: center;
				font-size: 13px;
				border-bottom: 1px solid #efefef;
			}
			.history-list{
				display: flex;
				flex-wrap: wrap;
				.uni-tag{
					margin-top: 5px;
					margin-right: 5px;
				}
				
			}
		}
		
</style>
